const getStudentListByClassIdUrl = require('../../../config.js').getStudentListByClassIdUrl // 获取班级学生
const getCLassList = require('../../../config.js').getCLassList // 获取班级ID getAcceptTotal
const getAcceptTotal = require('../../../config.js').getAcceptTotal //  获取总数比

const config = require('../../../ajax.js');
let app = getApp()
let sellPhoneInfo = app.globalData.sellPhoneInfo
let teacherInfo = app.globalData.teacherInfo
let organizationId, staffId;
Page({
  data: {
    itemsProflie:[],
    sheight:sellPhoneInfo.height + 120,
    defalut: '../../../images/common/bg2.png',
    index: 0,
    isTab: false,
    classListArray: [], // 班级列表
    storageTeacherInfo: null,
    homeworkTotal: 0,
    studentTotal: 0
  },
  onLoad:function(){
    var that = this;
    var data = new Date();
    console.log(data.getDay())
    
    that.setData({
      year: data.getFullYear(),
      month: data.getMonth(),
      day: data.getDay()
    })
    wx.showLoading({
      title: '努力加载中...',
    })
    wx.getStorage({
      key: 'teacherInfo',
      success: function (res) {
        console.log(res)
        let storageTeacherInfo = res.data
        that.setData({
          storageTeacherInfo : storageTeacherInfo
        })
        let organizationId = res.data.organizationId
        let staffId = res.data.staffId
        let parms = {
          teacherId: staffId
        }
        console.log(staffId)
        that.apiGetClassId(parms)
      },
    })
  },
  // api 查、接受总数比 2.查、班级列表 3.查、学生列表ByClassId
  apiGetNum(parms){
    let that = this
    wx.request({
      url: getAcceptTotal,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: parms,
      success: (res) => {
        console.log(res)
        this.setData({
          homeworkTotal: res.data.data.homeworkTotal,
          studentTotal: res.data.data.studentTotal
        })
      },
      fail : (res) => {
        console.log(res)
      }
      
    })
  },
  apiGetClassId(parms) {
    wx.request({
      url: getCLassList,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: parms,
      success: (res) => {
        console.log(res.data.data)
        let classListArray = res.data.data
        this.setData({
          classListArray: classListArray
        })
        //  回调获取学生列表
        let classId = res.data.data[0].classId
        let data = {
          "classId": classId,
          "staffId": this.data.storageTeacherInfo.staffId,
          "organizationId": this.data.storageTeacherInfo.organizationId,
          "signInType": "ALL"
        }
        let data1 = {
          classId: classId
        }
        console.log(data)
        this.apiGetStudentList(data)
        this.apiGetNum(data1)
      },
      fail: (res) => {
        console.log(res)
      }
    })
  },
  apiGetStudentList(parms){
    wx.request({
      url: getStudentListByClassIdUrl,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: parms,
      success: (res) => {
        console.log(res)
        if(res.data.status == 200){
          wx.hideLoading()
          this.setData({
            itemsProflie: res.data.data
          })
        }
      },
      fail: (res) => {
        console.log(res)
      }
    })
  },
  //api END
  navHomeWorkMsg:function(e){
    console.log(e.currentTarget.dataset.item)
    var studentInfo = JSON.stringify(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: '../../views/userMenu/pages/meg/meg?studentId=' + studentInfo,
    })
  },
   // 切换显示学生的列表
  tabShowList:function(){
    let that = this
    let isTab = that.data.isTab
    that.setData({
      isTab: !isTab
    })
  },
  // 选择班级
  bindPickerChange(e){
    let index = e.detail.value
    console.log(this.data.classListArray[index].classId)
    let classId = this.data.classListArray[index].classId
    let parms = {
      "classId": classId,
      "staffId": this.data.storageTeacherInfo.staffId,
      "organizationId": this.data.storageTeacherInfo.organizationId,
      "signInType": "ALL"
    }
    let data = {
      classId: classId
    }
    this.apiGetStudentList(parms)
    this.apiGetNum(data)
    this.setData({
      index: e.detail.value
    })
  }
})